package com.gym.openapi.services.impl;

import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.dao.MenuMapper;
import com.gym.openapi.entity.Menu;
import com.gym.openapi.services.MenuServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ming
 * 2021/11/24
 * description:
 */
@Service
public class MenuServicesImpl implements MenuServices {
    @Autowired
    private MenuMapper menuMapper;


    /**
     * @author:ming
     * @Date: 2021/11/24
     * @description:四表联查menu、role_menu、user_role、admin_user
     */
    @Override
    public List<Menu> getMenuByUid(Integer id) {
        if (id > 0) {
            //获取用户可以访问的所有菜单
            List<Menu> all = menuMapper.getMenuByUid(id);
            //处理菜单的级别，级联，先找一级再往下循环查找
            List<Menu> fu = new ArrayList<>();
            for (Menu menu : all) {
                //父id为Null即为父级菜单(一级菜单)
                if (menu.getParentId() == null) {
                    //找到父菜单，添加
                    fu.add(menu);
                }
            }
            //继续循环查找二级菜单
            for (Menu menu : fu) {
                List<Menu> erzis = new ArrayList<>();
                for (Menu zi : all) {
                    //zi.getType() != 不需要显示增删改按钮
                    if (zi.getParentId() != null && zi.getParentId().equals(menu.getId()) && zi.getType() != 2) {
                        //找到子菜单，添加
                        erzis.add(zi);
                    }
                }
                //把子菜单赋值给父对象
                menu.setChildren(erzis);
            }
            return fu;
        }
        return null;
    }

    /**
     * @author:ming
     * @Date: 2021/11/24
     * @description:显示授权菜单树
     */
    @Override
    public List<Menu> getAllTree() {
        List<Menu> all = menuMapper.getAllMenu();
        //找到在授权中要显示的一级菜单
        List<Menu> fu1 = new ArrayList<>();
        for (Menu menu : all) {
            if (menu.getParentId() == null) {
                fu1.add(menu);
            }
        }
        //遍历父节点
        for (Menu menu1 : fu1) {
            //创建子节点的集合，二级菜单
            List<Menu> zi1 = new ArrayList<>();
            for (Menu menu2 : all) {
                if (menu2.getParentId() != null && menu2.getParentId().equals(menu1.getId())) {
                    zi1.add(menu2);
                }
            }
            //遍历所有菜单节点,找一级菜单的子菜单
            //把子菜单集合赋值给一级踩单车节点
            menu1.setChildren(zi1);
        }
        return fu1;
    }

    /**
     * @author:ming
     * @Date: 2021/11/24
     * @description:查找子菜单方法2 steam
     */
//    public Menu findChildren(Menu m, List<Menu> all) {
//        List<Menu> collect = all.stream().
//                filter(x -> x.getParentId() != null && m.getId().equals(x.getParentId()) && x.getType() != 2).
//                map(x -> findChildren(x, all)).
//                collect(Collectors.toList());
//        m.setChildren(collect);
//        return m;
//    }

    /**
     *@author:ming
     *@Date: 2021/11/27
     *@description:菜单页面管理显示
     */
    @Override
    public ResultTable getList(Integer page, Integer limit) {
        List<Menu> mlist = menuMapper.getList();
        return new ResultTable(0, "", mlist.size(),mlist);
    }

    /**
     *@author:ming
     *@Date: 2021/11/27
     *@description:菜单页面管理权限根据id显示
     */
    @Override
    public Menu getMenuById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResponseResult update(Menu menu) {
        if (menu!=null) {
            int i = menuMapper.updateByPrimaryKeySelective(menu);
            if (i>0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("动不了菜单哦");
    }

    @Override
    public ResponseResult add(Menu menu) {
        if (menu!=null) {
            int i = menuMapper.insertSelective(menu);
            if (i>0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("添加失败");
    }


}
